package nju.bean.meeting.clientManagement;

import java.io.IOException;

import nju.bean.meeting.data.User;
import nju.bean.meeting.net.ClientSimpleBearer;
import nju.bean.meeting.transporter.Feedback.GetUserFeedback;
import nju.bean.meeting.transporter.Feedback.JudgeFeedback;
import nju.bean.meeting.transporter.Feedback.StatusFeedback;
import nju.bean.meeting.transporter.Request.CheckNameRequest;
import nju.bean.meeting.transporter.Request.GetUserRequest;
import nju.bean.meeting.transporter.Request.LogInRequest;
import nju.bean.meeting.transporter.Request.Request;
import nju.bean.meeting.transporter.Request.SubmitUserRequest;

public class UserClient {
	
	static private UserClient instance;
	
	private ClientSimpleBearer inforSender;
	
	private UserClient(){
		
	}
	
	public static UserClient getInstance(){
		if(instance == null)
			instance = new UserClient();
		return instance;
	}
	
	public void setBearer(ClientSimpleBearer inforSender){
		this.inforSender = inforSender;
	}
	
	public boolean sendUserName(String userName) throws IOException{
		Object ob = inforSender.sendRequest(new CheckNameRequest(userName));
		JudgeFeedback jf = (JudgeFeedback)ob;
		return jf.getResult();
	}
	
	public int sendPassword(String userName,String password) throws IOException{
		Request lr = new LogInRequest(userName,password);
		Object ob = inforSender.sendRequest(lr);
		StatusFeedback jf = (StatusFeedback)ob;	
		return jf.getStatus();
	}
	
	public boolean sendUserInfor(User user) throws IOException{
		System.out.println(user.getUserName());
		Request sur = new SubmitUserRequest(user);
		Object ob =inforSender.sendRequest(sur);
		JudgeFeedback jf = (JudgeFeedback)ob;
		return jf.getResult();
	}
	
	public User getUserInfor(String userName) throws IOException{		
		Request gur = new GetUserRequest(userName);
		GetUserFeedback guf = (GetUserFeedback)inforSender.sendRequest(gur);
		return guf.getUser();
	}
	
}
